﻿<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Imagin.Controls.Common"
    xmlns:Common.Data.Converters="clr-namespace:Imagin.Common.Data.Converters;assembly=Imagin.Common"
    xmlns:Controls.Common.Extensions="clr-namespace:Imagin.Controls.Common.Extensions;assembly=Imagin.Controls.Common">
    <Common.Data.Converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
    
    <Style TargetType="{x:Type local:TreeViewColumnHeader}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:TreeViewColumnHeader}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <Border
                            Grid.ColumnSpan="2" 
                            BorderThickness="{TemplateBinding BorderThickness}" 
                            Background="{TemplateBinding Background}" 
                            BorderBrush="{TemplateBinding BorderBrush}" />
                        <ContentPresenter
                            Margin="6,3" 
                            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        <Path
                            Grid.Column="1" 
                            x:Name="SortArrow" 
                            Data="M0,0 L1,0 0.5,1 z" 
                            Fill="{TemplateBinding Foreground}"
                            Height="6" 
                            Margin="0,0,8,0"  
                            RenderTransformOrigin="0.5,0.4"
                            Stretch="Fill" 
                            VerticalAlignment="Center" 
                            Visibility="Collapsed" 
                            Width="8"/>
                        <Thumb 
                            Grid.ColumnSpan="2" 
                            x:Name="PART_RightHeaderGripper" 
                            Cursor="SizeWE"
                            Margin="-8,0,-9,0"
                            HorizontalAlignment="Right"
                            Style="{TemplateBinding GripperStyle}"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="SortDirection" Value="Ascending">
                            <Setter TargetName="SortArrow" Property="Visibility" Value="Visible" />
                            <Setter TargetName="SortArrow" Property="RenderTransform">
                                <Setter.Value>
                                    <RotateTransform Angle="180" />
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <Trigger Property="SortDirection" Value="Descending">
                            <Setter TargetName="SortArrow" Property="Visibility" Value="Visible" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="Style.TreeViewExt.ScrollViewer" TargetType="ScrollViewer">
        <Setter Property="CanContentScroll" Value="True"/>
        <Setter Property="VerticalScrollBarVisibility" Value="Auto"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ScrollViewer">
                    <Border 
                        Background="{TemplateBinding Background}" 
                        BorderBrush="{TemplateBinding BorderBrush}" 
                        BorderThickness="{TemplateBinding BorderThickness}">
                        <Grid Background="{TemplateBinding Background}">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="*"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            
                            <local:TreeViewColumnHeadersPresenter
                                Grid.Row="0"
                                Grid.ColumnSpan="2"
                                Columns="{Binding TemplatedParent.Columns, RelativeSource={RelativeSource TemplatedParent}}"
                                ContextMenu="{Binding TemplatedParent.ColumnHeaderContextMenu, RelativeSource={RelativeSource TemplatedParent}}"
                                Height="{Binding TemplatedParent.ColumnHeaderHeight, RelativeSource={RelativeSource TemplatedParent}}"
                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                Visibility="{Binding TemplatedParent.ColumnHeaderVisibility, RelativeSource={RelativeSource TemplatedParent}}">
                                <local:TreeViewColumnHeadersPresenter.ItemTemplate>
                                    <DataTemplate>
                                        <local:TreeViewColumnHeader
                                            ColumnWidth="{Binding Width}"
                                            Content="{Binding}"
                                            ContentStringFormat="{Binding TemplatedParent.ColumnHeaderStringFormat, RelativeSource={RelativeSource AncestorType={x:Type ScrollViewer}}}"
                                            ContentTemplate="{Binding TemplatedParent.ColumnHeaderTemplate, RelativeSource={RelativeSource AncestorType={x:Type ScrollViewer}}}"
                                            ContentTemplateSelector="{Binding TemplatedParent.ColumnHeaderTemplateSelector, RelativeSource={RelativeSource AncestorType={x:Type ScrollViewer}}}"
                                            HorizontalContentAlignment="{Binding HorizontalContentAlignment}"
                                            SortDirection="{Binding SortDirection}"
                                            VerticalContentAlignment="{Binding VerticalContentAlignment}"/>
                                    </DataTemplate>
                                </local:TreeViewColumnHeadersPresenter.ItemTemplate>
                            </local:TreeViewColumnHeadersPresenter>
                            
                            <ScrollContentPresenter 
                                Grid.ColumnSpan="2"
                                Grid.Row="1" 
                                x:Name="PART_ScrollContentPresenter" 
                                KeyboardNavigation.DirectionalNavigation="Local"
                                Margin="{TemplateBinding Padding}"/>
                            
                            <ScrollBar 
                                Grid.Column="1"
                                Grid.Row="1"
                                Name="PART_VerticalScrollBar"
                                Maximum="{TemplateBinding ScrollableHeight}"
                                ViewportSize="{TemplateBinding ViewportHeight}"
                                Value="{TemplateBinding VerticalOffset}"
                                Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"/>
                            <ScrollBar 
                                Grid.Row="2"
                                Name="PART_HorizontalScrollBar"
                                Orientation="Horizontal"
                                Maximum="{TemplateBinding ScrollableWidth}"
                                ViewportSize="{TemplateBinding ViewportWidth}"
                                Value="{TemplateBinding HorizontalOffset}"
                                Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"/>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="{x:Type local:TreeViewExt}" TargetType="{x:Type local:TreeViewExt}">
        <Setter Property="Controls.Common.Extensions:ItemsControlExtensions.SelectNoneOnEmptySpaceClick" Value="True"/>
        <Setter Property="Controls.Common.Extensions:ItemsControlExtensions.CanDragSelect" Value="True"/>
        <Setter Property="HorizontalAlignment" Value="Stretch"/>
        <Setter Property="VerticalAlignment" Value="Stretch"/>
        <Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="True"/>
        <Setter Property="VirtualizingStackPanel.VirtualizationMode" Value="Recycling"/>
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:TreeViewExt}">
                        <ScrollViewer 
                            x:Name="PART_ScrollViewer"
                            Background="{TemplateBinding Background}" 
                            BorderBrush="{TemplateBinding BorderBrush}" 
                            BorderThickness="{TemplateBinding BorderThickness}" 
                            CanContentScroll="True"
                            Focusable="False" 
                            Padding="{TemplateBinding Padding}" 
                            Style="{StaticResource Style.TreeViewExt.ScrollViewer}">
                            <Grid>
                                <ItemsPresenter HorizontalAlignment="Stretch"/>
                            </Grid>
                    </ScrollViewer>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel IsItemsHost="True"/>
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>